package com.jdy.haoduoaiteacher;

import android.content.Context;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.support.multidex.MultiDex;

import com.jdy.haoduoai.LeXiaoXiaoBanApp;
import com.jdy.haoduoaiteacher.audioconverter.AndroidAudioConverter;
import com.jdy.haoduoaiteacher.audioconverter.callback.ILoadCallback;
import com.jdy.haoduoaiteacher.model.LeMachine;
import com.jdy.haoduoaiteacher.model.LeUser;
import com.jdy.haoduoaiteacher.util.CustomConstants;
import com.jdy.haoduoaiteacher.util.LeConfig;
import com.jdy.haoduoaiteacher.util.LePreference;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.ycsj.goldmedalnewconcept.R;

import java.util.Map;

//import com.jdy.haoduoaiteacher.rx.util.async.Async;
//import com.jdy.haoduoaiteacher.util.ImHelper;

//import rx.functions.Action1;
//import rx.functions.Func0;
//import rx.functions.Func1;
//import com.squareup.leakcanary.LeakCanary;

public class MyTeacherApp extends LeXiaoXiaoBanApp {

  @SuppressWarnings("unused")
  private final boolean isDebug = false;

  public static MyTeacherApp instance;
  private static Context context;// static way to get context of this app!
  public static String ordernumber;

//  private ImHelper mImHelper;

  public int  screenW, screenH;

  private int collectionId;
  private int orderId;

  private LeUser mLeUser;
  private LeMachine mLeBaby;
  private DisplayImageOptions options;

  private int growth_period;

  public String kidAge;

  private void removeTempFromPref() {
    SharedPreferences sp = getSharedPreferences(
        CustomConstants.APPLICATION_NAME, MODE_PRIVATE);
    sp.edit().remove(CustomConstants.PREF_TEMP_IMAGES).commit();
  }

//  public ImHelper getImHelper() {
//    return mImHelper;
//  }

  public int getGrowth_period() {
    return growth_period;
  }

  public void setGrowth_period(int growth_period) {
    this.growth_period = growth_period;
  }

  public DisplayImageOptions getDisplayImageOptions() {
    return options;
  }

  public int getCollectionId() {
    return collectionId;
  }

  public void setCollectionId(int collectionId) {
    this.collectionId = collectionId;
  }

  public int getOrderId() {
    return orderId;
  }

  public void setOrderId(int orderId) {
    this.orderId = orderId;
  }

  public static MyTeacherApp getInstance() {
    return instance;
  }

  public static Map<String, Object> globalMeta = null;

  private void readConfig() {
//
//    Async.start(new Func0<String>() {
//
//      @Override
//      public String call() {
//
//        byte[] data = FileManager.ReadDataFromAssetfile("meta_1.json");
//        String json = new String(data);
//        return json;
//
//      }
//    }).filter(new Func1<String, Boolean>() {
//
//      @Override
//      public Boolean call(String result) {
//        return Tools.isNotEmpty(result);
//      }
//    }).subscribe(new Action1<String>() {
//      @SuppressWarnings("unchecked")
//      @Override
//      public void call(String json) {
//        if (Tools.isNotEmpty(json)) {
//          globalMeta = JsonSerializer.getInstance()
//              .deserialize(json, Map.class);
//        }
//
//      }
//    });
  }

  @Override
  protected void attachBaseContext(Context base) {
    super.attachBaseContext(base);
    MultiDex.install(this);
  }

  @Override
  public void onCreate() {
    super.onCreate();

    //LeakCanary.install(this);

    MyTeacherApp.context = getApplicationContext();
    LePreference.getInstance().setContext(MyTeacherApp.context);
    instance = this;
    mLeUser = null;
    mLeBaby = null;

    LeConfig.PlayMode = LePreference.getInstance().getInt("playmode", 3);

    initImageLoader();
    readConfig();
//    mImHelper = new ImHelper();

    removeTempFromPref();

    // FFMpegService.getInstance().loadFFMpegBinary();

    AndroidAudioConverter.load(this, new ILoadCallback() {
      @Override
      public void onSuccess() {
        // Great!
      }
      @Override
      public void onFailure(Exception error) {
        // FFmpeg is not supported by device
        error.printStackTrace();
      }
    });
  }

  private void initImageLoader() {
    options = new DisplayImageOptions.Builder()
        .showImageOnFail(R.drawable.default_img_listitem) // 下载 失败 显示的 图片
        .cacheInMemory(true) // 缓存
        .bitmapConfig(Bitmap.Config.RGB_565) // 图片格式
        .build();
    ImageLoaderConfiguration configuration = new ImageLoaderConfiguration.Builder(
        getApplicationContext()).writeDebugLogs().build();
    ImageLoader.getInstance().init(configuration);
  }

  public LeMachine getBabyTeacher() {
    return mLeBaby;
  }

  public LeUser getUserTeacher() {
    return mLeUser;
  }

  public void setUser(LeUser lu) {
    this.mLeUser = lu;
  }

  public void setBaby(LeMachine lb) {
    this.mLeBaby = lb;
  }

  public void forgetUser() {
    collectionId = 0;
    orderId = 0;
    this.mLeUser = null;
    SharedPreferences prefs = this.getSharedPreferences(Constant.USER_INFO,
        Context.MODE_PRIVATE);
    prefs.edit().clear().commit();

    LeConfig.isLogin = false;

    LePreference.getInstance().delete("usersig");
    LePreference.getInstance().delete("selectedpainpoint");
    LePreference.getInstance().delete("lastLaunchTime");

//    ImHelper ih = MyTeacherApp.getInstance().getImHelper();
//    if (ih != null) {
//      ih.logout();
//    }
  }

  public static Context getAppContext() {
    return MyTeacherApp.context;
  }

}
